package com.example.servlet;

import com.example.bean.Notice;
import com.example.dao.NoticeDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/publishNotice")
public class PublishNoticeServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String publisher = request.getParameter("publisher");

        Notice notice = new Notice();
        notice.setTitle(title);
        notice.setContent(content);
        notice.setPublishTime(new Date());
        notice.setPublisher(publisher);

        NoticeDAO dao = new NoticeDAO();
        boolean isPublished = dao.publishNotice(notice);

        if (isPublished) {
            response.sendRedirect("noticeList.jsp");
        } else {
            request.setAttribute("error", "公告发布失败，请稍后再试");
            request.getRequestDispatcher("publishNotice.jsp").forward(request, response);
        }
    }
}